/*
' Copyright (c) 2014 Diadrasis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using net.diadrasis.dnn.jsImageGallery.Components;
using DotNetNuke.Common;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.FileSystem;
using System.Text.RegularExpressions;

namespace net.diadrasis.dnn.jsImageGallery
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditjsImageGallery class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from jsImageGalleryModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class cEdit : jsImageGalleryModuleBase
    {

        public static string img; //the image from the querystring
        public static int imgID; //the image id of the db
        public static int secId; //the section id
        public static string imgsec;
        
        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                
                if (Page.IsPostBack == false)
                {
                    img = Request.QueryString["imgid"];
                    imgsec = Request.QueryString["imgsecid"];
                    if (imgsec != null){
                        secId = Convert.ToInt32(imgsec);
                    } 

                    if (img != null) {
                        imgID= Convert.ToInt32(img);                        
                    } else {
                        imgID= -1;
                    }
                                       
                    //in case it is an edit load the values
                    if (imgID > 0)  {
                        cImage editImg = cImageController.GetImage(imgID);                        
                        urlcLink.Url = editImg.link;
                        txtDesc.Text = editImg.desc;
                        urlcFile.Url = editImg.file;
                    }
                    
                    
                    // myHyperLink.NavigateUrl=ModuleContext.NavigateUrl(PortalSettings.ActiveTab.TabID, "cEdit", "mid=" + ModuleId.ToString());
                    //myHyperLink.Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(", this, PortalSettings, true, false));
                    this.btnReturn.Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(Globals.NavigateURL("EditImages", "mid=" + this.ModuleId.ToString() + "&imgsecid="+ secId), this, PortalSettings, true, false));
                   
                }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        protected void btnOK_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            cImage img=new cImage();            
            img.id=imgID;
            img.imgsecID = secId;
            
            //the controler gives the file id in a string format "fileID=2;
            img.file = urlcFile.Url;
            
            //we get the url value of the file
            int dnnImgId=Convert.ToInt32(urlcFile.Url.Substring(7));
            FileInfo imgInfo=(FileInfo)FileManager.Instance.GetFile(dnnImgId);
            string fileUrl = PortalSettings.HomeDirectory + imgInfo.Folder + imgInfo.FileName;
            

            img.desc=txtDesc.Text;

            //get url from 
            img.link=urlcLink.Url;
            string linkUrl=img.link;

            if (!img.link.Contains("http:")){
             linkUrl= Globals.NavigateURL(Convert.ToInt32(img.link));
            }           
                      
            img.modID = ModuleId;
            cImageController.SaveImage(img, TabId);
            //btnReturn_Click(btnReturn, null);
            //UrlUtils.PopUpUrl(Globals.NavigateURL("Edit", "mid=" + this.ModuleId.ToString()), this, PortalSettings, true, false);          

      
        }       

        protected void btnReturn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

        }  
    }

}